package c2.cross.business.rank.h;

import c2.cross.business.rank.CrossRankDao;
import cate.common.table.activity.crossrankactivity.row.CrossRankActivityBaseRow;
import cate.game.role.RoleSnapshot;
import lombok.Getter;
import lombok.Setter;

/**
 * 跨服排行处理器实现
 */
public class CrossRankActivityHandlerImpl extends BaseSingleValueRankHandler {
    /**
     * 活动配置ID
     */
    @Getter
    @Setter
    private int configTid;

    private int condition;

    private int rankMax;

    public CrossRankActivityHandlerImpl(CrossRankActivityBaseRow activityBaseRow, CrossRankDao dao) {
        super(activityBaseRow.rankId, dao);
        this.condition = activityBaseRow.condition;
        this.rankMax = activityBaseRow.rankMax;
        fixData();
    }

    /**
     * 修复数据
     */
    private void fixData() {
        tryGetRankList().removeIf(rank -> rank.value < condition);
    }

    @Override
    public String getTag() {
        return "cross_rank_act_" + this.getRankId();
    }

    @Override
    protected int getRankNum() {
        return rankMax;
    }

    /**
     * 是否激活中
     */
    public boolean active() {
        return configTid > 0;
    }

    @Override
    public synchronized void record(RoleSnapshot snapshot, long newValue) {
        if (!active() || newValue < condition) {
            return;
        }
        super.record(snapshot, newValue);
    }
}
